package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import net.sf.json.JSONSerializer; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import java.text.DateFormat; 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.List;

import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiam;
import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiamService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoan;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoanService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLai;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLaiService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienBienLaiMonHoc;

import com.lb.jwitter.service.TblQLDTTXAHocVienBienLaiMonHocService;

import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienHocPhiBienLaiInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public Long getId() {
		return id;
	}

	public void setId(Long id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private Collection<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public Collection<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private int hocKyId; 
    public void setHocKyId(int hocKyId) {
       this.hocKyId = hocKyId;
 }

    public int getHocKyId() {
       return hocKyId;
 }

    private Collection<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public Collection<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private String soBienLai; 
    public void setSoBienLai(String soBienLai) {
       this.soBienLai = soBienLai;
 }

    public String getSoBienLai() {
       return soBienLai;
 }

    private String ngayLap; 
    public void setNgayLap(String ngayLap) {
       this.ngayLap = ngayLap;
 }

    public String getNgayLap() {
       return ngayLap;
 }

    private double soTien; 
    public void setSoTien(double soTien) {
       this.soTien = soTien;
 }

    public double getSoTien() {
       return soTien;
 }
    
    private double taiKhoan; 
    public void setTaiKhoan(double taiKhoan) {
       this.taiKhoan = taiKhoan;
 }

    public double getTaiKhoan() {
       return taiKhoan;
 }
  
    private double tienPhaiDong; 
    public void setTienPhaiDong(double tienPhaiDong) {
       this.tienPhaiDong = tienPhaiDong;
 }

    public double getTienPhaiDong() {
       return tienPhaiDong;
 }
    
    private double soTienPhaiDong; 
    public void setSoTienPhaiDong(double soTienPhaiDong) {
       this.soTienPhaiDong = soTienPhaiDong;
 }

    public double getSoTienPhaiDong() {
       return soTienPhaiDong;
 }
    
    private double soDuTK; 
    public void setSoDuTK(double soDuTK) {
       this.soDuTK = soDuTK;
 }

    public double getSoDuTK() {
       return soDuTK;
 }

    private String dienGiai; 
    public void setDienGiai(String dienGiai) {
       this.dienGiai = dienGiai;
 }

    public String getDienGiai() {
       return dienGiai;
 }
    
    private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService;
    private TblQLDTTXAHocVienMienGiamService tblQLDTTXAHocVienMienGiamService;
    private TblQLDTTXAHocVienTaiKhoanService tblQLDTTXAHocVienTaiKhoanService;

    private TblQLDTTXAHocVienHocPhiBienLai tblQLDTTXAHocVienHocPhiBienLai; 
    public TblQLDTTXAHocVienHocPhiBienLai  getTblQLDTTXAHocVienHocPhiBienLai() {
        return tblQLDTTXAHocVienHocPhiBienLai;
 }

    private Collection<TblQLDTTXAHocVienHocPhiBienLai> tblQLDTTXAHocVienHocPhiBienLais; 
    public Collection<TblQLDTTXAHocVienHocPhiBienLai>  getTblQLDTTXAHocVienHocPhiBienLais() {
         return tblQLDTTXAHocVienHocPhiBienLais; 
      } 

    private TblQLDTTXAHocVienHocPhiBienLaiService tblQLDTTXAHocVienHocPhiBienLaiService; 

    private String tblQLDTTXAHocVienBienLaiMonHocStr; 
    public void setTblQLDTTXAHocVienBienLaiMonHocStr(String tblQLDTTXAHocVienBienLaiMonHocStr) 
{
       this.tblQLDTTXAHocVienBienLaiMonHocStr = tblQLDTTXAHocVienBienLaiMonHocStr;
 }

    public String getTblQLDTTXAHocVienBienLaiMonHocStr() 
 {
       return tblQLDTTXAHocVienBienLaiMonHocStr;
 }

    private TblQLDTTXAHocVienBienLaiMonHocService tblQLDTTXAHocVienBienLaiMonHocService; 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

	private int preInsert(Message msg, TblQLDTTXAHocVienHocPhiBienLai obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXAHocVienHocPhiBienLai obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
	     Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
	    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_HocPhi_BienLai", "them")){ 
	        return "noprivilege"; 
	    }        
	    
    	tblQLDTTXAHocVienHocPhiBienLaiService = (TblQLDTTXAHocVienHocPhiBienLaiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhi_BienLaiClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXAHocVienBienLaiMonHocService = (TblQLDTTXAHocVienBienLaiMonHocService) context.getBean("tbl_QLDTTXA_HocVien_BienLai_MonHocClient"); 
        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 
	    tblQLDTTXAHocVienMienGiamService = (TblQLDTTXAHocVienMienGiamService) context.getBean("tbl_QLDTTXA_HocVien_MienGiamClient");
	    tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
	    tblQLDTTXAHocVienTaiKhoanService = (TblQLDTTXAHocVienTaiKhoanService) context.getBean("tbl_QLDTTXA_HocVien_TaiKhoanClient");
	    

        msg = new Message(); 
        tblQLDTTXAHocVienHocPhiBienLai = new TblQLDTTXAHocVienHocPhiBienLai(); 
        tblQLDTTXAHocVienHocPhiBienLai.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId))); 
        tblQLDTTXAHocVienHocPhiBienLai.setHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(hocKyId))); 
        tblQLDTTXAHocVienHocPhiBienLai.setSoBienLai(soBienLai); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayLap); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXAHocVienHocPhiBienLai.setNgayLap(d); 
        tblQLDTTXAHocVienHocPhiBienLai.setSoTien(soTien); 
        
        DecimalFormat myFormatter = new DecimalFormat("###,###.##");
        Date today = new Date();
        if(dienGiai == null)
        	dienGiai = "";
        dienGiai += "<br/>-------------------------------- ";
        dienGiai += "<br/><i>Inserted at " + (new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(today)) + " by " + taiKhoanDangNhap.getUsername() + "(" + taiKhoanDangNhap.getCanBoId().getTen() + ")</i> ";
        dienGiai += "<br/>Tài khoản trước khi lập biên lai: " + myFormatter.format(taiKhoan);
        dienGiai += "<br/>Học phí học kỳ: " + myFormatter.format(tienPhaiDong);
        dienGiai += "<br/>Số tiền phải đóng: " + myFormatter.format(soTienPhaiDong);
        dienGiai += "<br/>Số tiền thu: " + myFormatter.format(soTien);
        dienGiai += "<br/>Tài khoản sau khi lập biên lai: " + myFormatter.format(soDuTK);
        //System.out.println(dienGiai);
        tblQLDTTXAHocVienHocPhiBienLai.setDienGiai(dienGiai); 
        tblQLDTTXAHocVienHocPhiBienLai.setCanBoId(taiKhoanDangNhap.getCanBoId());
        preInsert(msg,tblQLDTTXAHocVienHocPhiBienLai); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblQLDTTXAHocVienHocPhiBienLaiService.createtblQLDTTXAHocVienHocPhiBienLai(tblQLDTTXAHocVienHocPhiBienLai); 
           postInsert(msg, tblQLDTTXAHocVienHocPhiBienLai); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblQLDTTXAHocVienHocPhiBienLai.setId(id);
       dienGiai += "<br/>Môn học đóng hp: ";  
		JSONArray jsonTblQLDTTXAHocVienBienLaiMonHoc = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienBienLaiMonHocStr); 
		for (int i = 0; i < jsonTblQLDTTXAHocVienBienLaiMonHoc.size(); i++) 
		{ 
		      	JSONObject jsObj = jsonTblQLDTTXAHocVienBienLaiMonHoc.getJSONObject(i);
		      	TblQLDTTXAHocVienHocPhiMonHoc tblQLDTTXAHocVienHocPhiMonHoc 
		      		= tblQLDTTXAHocVienHocPhiMonHocService
		      			.gettblQLDTTXAHocVienHocPhiMonHoc(new Long(jsObj.getInt("hocVienHocPhiMonHocId")));
		      	
		      	if(tblQLDTTXAHocVienHocPhiMonHoc != null){
			      	//cap nhat hoc phi trangThai da dong hP
			      	tblQLDTTXAHocVienHocPhiMonHoc.setDaDongHP(true);
			      	tblQLDTTXAHocVienHocPhiMonHocService.updatetblQLDTTXAHocVienHocPhiMonHoc(tblQLDTTXAHocVienHocPhiMonHoc);
			      	
			      	//cap nhat hocVien Mien giam trang thai daXetHP
			      	if(tblQLDTTXAHocVienHocPhiMonHoc.isMienMon()){
			      		List<ConditionReport> cons = new ArrayList<ConditionReport> ();
			      		
			      		ConditionReport con = new ConditionReport();
			      		con.setCol("daXetHP");
			      		con.setOperator("eq");
			      		con.setValue1(false);
			      		cons.add(con);
			      		
			      		ConditionReport con2 = new ConditionReport();
			      		con2.setCol("donMGId.hocVienId.id");
			      		con2.setOperator("eq");
			      		con2.setValue1(tblQLDTTXAHocVienHocPhiBienLai.getHocVienId().getId());
			      		cons.add(con2);
			      		
			      		ConditionReport con3 = new ConditionReport();
			      		con3.setCol("monHocId.id");
			      		con3.setOperator("eq");
			      		con3.setValue1(tblQLDTTXAHocVienHocPhiMonHoc.getMonHocId().getId());
			      		cons.add(con3);
			      		
			      		ConditionReport con4 = new ConditionReport();
			      		con4.setCol("donMGId.trangThaiId");
			      		con4.setOperator("eq");
			      		con4.setValue1(2);
			      		cons.add(con4);
			      		
			      		
			      		List<TblQLDTTXAHocVienMienGiam> tblHocVienMienGiams = tblQLDTTXAHocVienMienGiamService.findtblQLDTTXAHocVienMienGiamByCondition(cons, 0, 0);
			      		for(TblQLDTTXAHocVienMienGiam hocVienMienGiam: tblHocVienMienGiams){
			      			hocVienMienGiam.setDaXetHP(true);
			      			tblQLDTTXAHocVienMienGiamService.updatetblQLDTTXAHocVienMienGiam(hocVienMienGiam);
			      		}
			      	}
			      	
			      	//them chi tiet bien lai
			        TblQLDTTXAHocVienBienLaiMonHoc obj0 = new TblQLDTTXAHocVienBienLaiMonHoc(); 
			        obj0.setBienLaiId(tblQLDTTXAHocVienHocPhiBienLai); 
			        obj0.setMonHocId(tblQLDTTXAHocVienHocPhiMonHoc.getMonHocId()); 
			        tblQLDTTXAHocVienBienLaiMonHocService.createtblQLDTTXAHocVienBienLaiMonHoc(obj0);
			        dienGiai += (obj0.getMonHocId().getTen() != null ? obj0.getMonHocId().getTen() : obj0.getMonHocId().getId().toString()) + ", ";
			        
		      	}
		} 
		tblQLDTTXAHocVienHocPhiBienLai.setDienGiai(dienGiai);
		tblQLDTTXAHocVienHocPhiBienLaiService.updatetblQLDTTXAHocVienHocPhiBienLai(tblQLDTTXAHocVienHocPhiBienLai);
		
		List<ConditionReport> cons = new ArrayList<ConditionReport> ();
  		
  		ConditionReport con = new ConditionReport();
  		con.setCol("hocVienId.id");
  		con.setOperator("eq");
  		con.setValue1(new Long(hocVienId));
  		cons.add(con);
		List<TblQLDTTXAHocVienTaiKhoan> tblQLDTTXAHocVienTaiKhoans = tblQLDTTXAHocVienTaiKhoanService.findtblQLDTTXAHocVienTaiKhoanByCondition(cons, 0, 0);
		if (tblQLDTTXAHocVienTaiKhoans != null && tblQLDTTXAHocVienTaiKhoans.size() > 0){
			TblQLDTTXAHocVienTaiKhoan tblQLDTTXAHocVienTaiKhoan = tblQLDTTXAHocVienTaiKhoans.get(0);
			tblQLDTTXAHocVienTaiKhoan.setSoDuTaiKhoan(soDuTK);
			tblQLDTTXAHocVienTaiKhoanService.updatetblQLDTTXAHocVienTaiKhoan(tblQLDTTXAHocVienTaiKhoan);
		}
		else{
			TblQLDTTXAHocVienTaiKhoan tblQLDTTXAHocVienTaiKhoan = new TblQLDTTXAHocVienTaiKhoan();
			tblQLDTTXAHocVienTaiKhoan.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId)));
			tblQLDTTXAHocVienTaiKhoan.setSoDuTaiKhoan(soDuTK);
			tblQLDTTXAHocVienTaiKhoanService.createtblQLDTTXAHocVienTaiKhoan(tblQLDTTXAHocVienTaiKhoan);
		}
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_HocVien_HocPhi_BienLai Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
